***** RUN DO_MASTER.do ***** do "$Do_files\DO_MASTER.do"

global program "OA_FigureB35"
cap log close
cap log using "$Log_files\log_$program$today", replace

**** REPLICATION OF FIGURE B.3.5 ****

clear all
set mem 2g
set more off

// Import data

use $Data\data_news_voting, clear

// Get controls

global ctrl_base	"sh_lde lpop_A sh_F sh_F_new sh_cspsup sh_sec sh_ter lmean_reink lmean_reink2 charact_altitude charact_altitude_std sh_active sh_young sh_prot sh_M mk com_property"
global ctrl_plus	"mean_votation lcrSR_F_N"
global fe			"charact_agglo"
global fe_iv		"I_charact_agglo_*"
global cluster		"charact_agglo"
 
// Macros

global export "$Results\\$program.xls"
capture erase "$Results\\$program.txt" 
capture erase "$export"

// Regressions: Before votation
	
forvalues i=11(-1)2 {
	g endog = endog_`i'm
	cap drop lcrSR_F_N
	g lcrSR_F_N = lcrSR_F_N_`i'm
	ivreg2 oui (endog = instr_dev_`i'm) ///  
			$ctrl_plus ///
			$ctrl_base ///
			$fe_iv ///
			if sample_hq00dstr==1, ///
			cluster($cluster) savefprefix(first)  partial($fe_iv) // first
	outreg2 using "$export", nocons addstat(F test (first stage), e(widstat)) dec(1) keep(endog)
	drop endog lcrSR_F_N
}

// Regressions: After votation

forvalues i=1(1)3 {
	g endog = endog_`i'f
	cap drop lcrSR_F_N
	g lcrSR_F_N = lcrSR_F_N_`i'f
	ivreg2 oui (endog = instr_dev_`i'f) ///  
			$ctrl_plus ///
			$ctrl_base ///
			$fe_iv ///
			if sample_hq00dstr==1, ///
			cluster($cluster) savefprefix(first) partial($fe_iv) // first
	outreg2 using "$export", nocons addstat(F test (first stage), e(widstat)) dec(1) keep(endog)
	drop endog lcrSR_F_N
}		

// Get point estimates

insheet using "$Results\\$program.txt", clear
drop v1
keep if _n == 4
gen id = _n
reshape long v, i(id) j(month)
replace month = month - 1
rename v beta
replace beta = subinstr(beta, "*", "", .) 
destring beta, replace
sort id
save "$Results\tmp_result_beta", replace

// Get s.e.

insheet using "$Results\\$program.txt", clear
drop v1
keep if _n == 5
gen id = _n
reshape long v, i(id) j(month)
replace month = month - 1
rename v 	se
replace se = subinstr(se, "(", "", .) 
replace se = subinstr(se, ")", "", .) 
replace se = subinstr(se, ",", "", .)
destring se, replace force
sort id
merge id using "$Results\tmp_result_beta"
drop _merge
sort id
save "$Results\tmp_result_beta", replace

// Get 90% C.I.

gen up95 = beta + invnormal(0.95)*se // 
gen lo95 = beta - invnormal(0.95)*se
*gen sd1_p = beta + 2*se
*gen sd1_n = beta - 2*se
gen y0 = 0

tsset month

// Correct post-votation months

replace month = month +2 if month > 10

// Coefficients on graph

tostring beta, g(b_str) force

// Make graph 

tw 	scatter beta month	if month != .	, msymbol(Oh) mcolor(navy) mlabel(b_str) mlabp(n) mlabs(vsmall) sort || ///
	line beta month 	if month != . 		, lcolor(navy) lwidth(medium) || ///
	line up95 month	 	if month != .		, fcolor (gs10) lcolor(navy) lpattern(dash) lwidth(medium) sort || ///
	line lo95 month 	if month != .		, fcolor (gs10) lcolor(navy) lpattern(dash) lwidth(medium) sort || ///
	(pcarrowi -3 11.25 8 11.25, lcolor(gs15) lwidth(vvvthick) mcolor(none)) ///
	(pcarrowi -3 11.75 8 11.75, lcolor(gs15) lwidth(vvvthick) mcolor(none)) ///
	(pcarrowi -3 12 8 12, lcolor(cranberry) lwidth(medthin) lpattern(dash) mcolor(none)) ///
	(pcarrowi 0 1 0 15, lcolor(cranberry) lwidth(medthin) mcolor(none)), ///
	yline(0, lcolor(cranberry) lwidth(medthin)) ///
	ytitle("2SLS Estimate Coefficient (90% C.I.)", margin(vsmall))   /// 
	ylab(-3(3)6, angle(0) format(%9.0f)) ///
	xtitle("Month Before/After Vote (Cumulative)", margin(medium))  ///
	xlabel(1 " " 2 "10" 3 " " 4 "8" 5 " " 6 "6" 7 " " 8 "4" 9 " " 10 "2" 11 " " 12 "0" 13 " " 14 "2" 15 " ", valuelabel) ///
	legend(off) bgcolor(white) graphregion(color(white)) 
graph export "$Results\\$program.pdf", as(pdf) replace

erase "$Results\tmp_result_beta.dta"
erase "$Results\\$program.txt"
erase "$Results\\$program.xls"

cap log close
